(function ($)
{

    $.post = function (postUrl, input, sucess_callback, mode)
    {
        if (typeof (mode) == 'undefined')
            mode = "json";

        verb = "POST";

        $.ajax({
            url: postUrl,
            type: verb,
            data: input,
            dataType: mode,
            contentType: "application/x-www-form-urlencoded",
            success: function (ret)
            {
                ret.params = input;
                if (typeof (sucess_callback) == 'function')
                    sucess_callback(ret);

            }
        });
    }

    $.session = function (key, callback)
    {
        $.post('./bus/get_session.php', 'key=' + key, callback);
    }

    $.createdAt = function (createdAt)
    {
        var ageInSeconds = createdAt; // (new Date().getTime() - new Date(createdAt).getTime()) / 1000;
        var s = function (n) { return n == 1 ? '' : 's' };
        if (ageInSeconds < 15)
        {
            return 'just now';
        }
        if (ageInSeconds < 60)
        {
            var n = ageInSeconds;
            return n + ' second' + s(n) + ' ago';
        }
        if (ageInSeconds < 60 * 60)
        {
            var n = Math.floor(ageInSeconds / 60);
            return n + ' minute' + s(n) + ' ago';
        }
        if (ageInSeconds < 60 * 60 * 24)
        {
            var n = Math.floor(ageInSeconds / 60 / 60);
            return n + ' hour' + s(n) + ' ago';
        }
        if (ageInSeconds < 60 * 60 * 24 * 7)
        {
            var n = Math.floor(ageInSeconds / 60 / 60 / 24);
            return n + ' day' + s(n) + ' ago';
        }
        if (ageInSeconds < 60 * 60 * 24 * 31)
        {
            var n = Math.floor(ageInSeconds / 60 / 60 / 24 / 7);
            return n + ' week' + s(n) + ' ago';
        }
        if (ageInSeconds < 60 * 60 * 24 * 365)
        {
            var n = Math.floor(ageInSeconds / 60 / 60 / 24 / 31);
            return n + ' month' + s(n) + ' ago';
        }
        var n = Math.floor(ageInSeconds / 60 / 60 / 24 / 365);
        return n + ' year' + s(n) + ' ago';
    }

    $.fn.setCursorPosition = function (pos)
    {
        if ($(this).get(0).setSelectionRange)
        {
            $(this).get(0).setSelectionRange(pos, pos);
        } else if ($(this).get(0).createTextRange)
        {
            var range = $(this).get(0).createTextRange();
            range.collapse(true);
            range.moveEnd('character', pos);
            range.moveStart('character', pos);
            range.select();
        }
    }

    $.fn.TextMask = function (initial, empty)
    {

        $t = $(this);

        if (initial != null && $t.val().length == 0)
        {
            $t.val(initial);
            $t.setCursorPosition(1, 1);
        }

        $t.keydown(function (e)
        {
            if ($(this).val() == empty || $(this).val() == initial)
                $(this).val("").css('color', '');
        })
	.keyup(function (e)
	{
	    if ($(this).val().length == 0 && empty != 'undefined')
	    {
	        $(this).val(empty).css('color', '#666');
	        $t.setCursorPosition(0, 1);
	    }
	})
	.focus(function ()
	{
	    if ($(this).val() == empty || $(this).val() == initial)
	        $(this).val("").css('color', '');
	})
	.blur(function ()
	{
	    if ($(this).val().length == 0)
	    {
	        $(this).val(initial).css('color', '#666');
	        $t.setCursorPosition(0, 1);
	    }
	});

        //$v.show();

        return $t;
    }

    $.bind = function (template, data)
    {
        var item;
        for (var i in data)
        {
            item = data[i];

            template.append(i + '<BR><BR>' + data[i] + '<BR><BR>' + typeof (data[i]) + '<BR><BR><BR><BR>');

        }
    }

    $.fn.verticalShow = function ()
    {
        $(this).show();
    }

    $.fn.verticalHide = function ()
    {
        $(this).hide();
    }

})(jQuery);
